package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Repository.ChipsServiceImpl;
import Server.ServerCore.Service.DataPacks.packRequest.bust_request;
import Server.ServerCore.Service.DataPacks.packRespose.bust_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;


import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-10
 *         Time: 下午9:13
 *         BustController =210
 *
 *         下注，买保险，双倍加注，玩家筹码减少；结算玩家牌，筹码不变，或则筹码增加
 */
@Service
public class BustController extends HandleController {

    private static final long serialVersionUID = -2283070420190657726L;

    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
        bust_request bustRequest= (bust_request) parseObject(buffer,bust_request.class);
        int circleUnitID=bustRequest.getCircleUnitID();
            player.atomicInteger.decrementAndGet();//爆牌，牌的数量减1

            if (player.atomicInteger.get()==0){
                player.setOver(true);
                player.setBet(false);
            }
            //爆牌且没有买保险的 ID
            CardResultStatusVO[] cardResultStatusVOs=player.getDeckoOfCardsVOFastList();
            if (cardResultStatusVOs!=null){
                CardResultStatusVO cardResultStatusVO=cardResultStatusVOs[circleUnitID];
                if (cardResultStatusVO.getInsurance()!=1){
                    cardResultStatusVO=null;
                    for (CardVO cardVO:player.getCardVOFastList()){
                        if (cardVO.getCircleUnitID()==circleUnitID){
                            player.getCardVOFastList().remove(cardVO);
                        }
                    }
                }
                if (circleUnitID==player.getNowCrileUnitID()&&circleUnitID!=5){
                    player.getDeckoOfCardsVOFastList()[circleUnitID].setBust((byte) 1);
                    bust_response bustResponse=new bust_response();
                    int now=PlayerManagerService.getNextcircleUnitID(player, circleUnitID);
                    bustResponse.setNextcircleUnitID(now);
                    if (now!=-1){
                        player.setNowCrileUnitID(now);
                    }
                    player.DirectSend(bustResponse.getJsonData(2,9));
                }
            }


    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[210]=this;//To change body of implemented methods use File | Settings | File Templates.
    }


}
